package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.ParkConfig;
import com.ruoyi.common.core.domain.entity.RuleConfig;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.ParkConfigMapper;
import com.ruoyi.system.mapper.RuleConfigMapper;
import com.ruoyi.system.service.IRuleConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 停车规则配置服务实现类
 *
 * @author ruoyi
 */
@Service
public class RuleConfigServiceImpl implements IRuleConfigService {

    @Autowired
    private RuleConfigMapper ruleConfigMapper;

    @Autowired
    private ParkConfigMapper parkConfigMapper;

    @Override
    public List<RuleConfig> selectRuleConfigList(RuleConfig ruleConfig) {
        return ruleConfigMapper.selectRuleConfigList(ruleConfig);
    }

    @Override
    public RuleConfig selectRuleConfigById(Long configId) {
        return ruleConfigMapper.selectRuleConfigById(configId);
    }

    @Override
    @Transactional
    public int insertRuleConfig(RuleConfig ruleConfig) {
        return ruleConfigMapper.insertRuleConfig(ruleConfig);
    }

    @Override
    @Transactional
    public int updateRuleConfig(RuleConfig ruleConfig) {
        return ruleConfigMapper.updateRuleConfig(ruleConfig);
    }

//    @Override
//    @Transactional
//    public int deleteRuleConfigByIds(Long[] configIds) {
//        return ruleConfigMapper.deleteRuleConfigByIds(configIds);
//    }

    @Override
    public boolean checkRuleNameUnique(RuleConfig ruleConfig) {
        Long configId = ruleConfig.getId() == null ? -1L : ruleConfig.getId();
        RuleConfig info = ruleConfigMapper.checkRuleNameUnique(ruleConfig.getRuleName());
        return info == null || info.getId().equals(configId);
    }

    @Override
    public void checkDataScope(Long configId) {
        // 校验当前用户是否有访问该规则的权限（通过创建人或部门）
        RuleConfig ruleConfig = ruleConfigMapper.selectRuleConfigById(configId);
        Long userId = SecurityUtils.getUserId();
        if (!userId.equals(ruleConfig.getCreateBy())) {
            throw new SecurityException("没有权限访问该规则配置");
        }
    }

//    @Override
//    public boolean hasAssociatedData(Long[] configIds) {
//        return false;
//    }

//    @Override
//    public boolean hasAssociatedData(Long[] configIds) {
//        // 查询是否有关联数据（如停车场、订单使用该规则）
//        return ruleConfigMapper.countAssociatedData(configIds) > 0;
//    }


    @Override
    public List<RuleConfig> selectAllEnabledRules() {
        RuleConfig ruleConfig = new RuleConfig();
       // ruleConfig.setDeleted(0); // 查询未删除的规则
        return ruleConfigMapper.selectRuleConfigList(ruleConfig);
    }

    @Override
    public boolean isRuleUsedByParkConfig(Long ruleId) {
        // 查询park_config表中是否存在使用该规则的记录
        ParkConfig param = new ParkConfig();
        param.setMemberRule(ruleId);
        List<ParkConfig> list = parkConfigMapper.selectParkConfigList(param);

        return !list.isEmpty(); // 如果列表不为空，说明存在引用
    }


    /**
     * 根据ID删除停车规则配置（逻辑删除或物理删除）
     * @param id 规则ID
     * @return 影响行数
     */
    @Override
    public int deleteRuleConfigById(Long id) {

         return ruleConfigMapper.deleteRuleConfigById(id);
    }
}
